package com.smarth.zhizulian.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.smarth.zhizulian.R;

/**
 * Created by 18865 on 2017/12/25.
 */

public class MessageDialog extends PopupWindow {
    private Activity mContext;
    private TextView tv_message;
    private TextView tv_cancel;
    private TextView tv_ok;

    public MessageDialog(Activity context) {
        this.mContext = context;
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.dialog_message, null);
        tv_message = (TextView) contentView.findViewById(R.id.tv_message);
        tv_cancel = (TextView) contentView.findViewById(R.id.tv_cancel);
        tv_ok = (TextView) contentView.findViewById(R.id.tv_ok);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MessageDialog.this.dismiss();
            }
        });

        this.setContentView(contentView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//        this.setFocusable(true);
//        this.setOutsideTouchable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
    }

    public void show() {
        showAtLocation(mContext.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        backgroundAlpha(0.5f);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        Activity activity = (Activity) mContext;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);
    }

    public MessageDialog setLeft(String text, final View.OnClickListener onClickListener) {
        tv_cancel.setText(text);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickListener != null) {
                    onClickListener.onClick(view);
                }
                MessageDialog.this.dismiss();
            }
        });
        return this;
    }

    public MessageDialog setRight(String text, final View.OnClickListener onClickListener) {
        tv_ok.setText(text);
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickListener != null) {
                    onClickListener.onClick(view);
                }
                MessageDialog.this.dismiss();
            }
        });
        return this;
    }

    public MessageDialog setMessage(String msg) {
        tv_message.setText(msg);
        return this;
    }
}
